package com.geor.gcv.core.process;

import java.util.ArrayList;
import java.util.List;

import org.opencv.core.Mat;

import com.geor.gcv.core.mat.ImageOperator;
import com.geor.gcv.core.util.DataTool;

/**
 * 自定义二值化的过程
 * 
 * @author geor.lusongnan
 *
 */
public class CustomThresholdProcess extends Process {

	Process chain;

	List<Mat> mats = new ArrayList<Mat>();

	int params;

	public void setParams(int params) {
		this.params = params;
	}

	public int getParams() {
		return params;
	}

	public CustomThresholdProcess(Process chain, int params) {
		this.chain = chain;
		setParams(params);
	}

	@Override
	public Mat process() {
		Mat mat = chain.process();

		final List<Integer> pexs = new ArrayList<Integer>();
		new ImageOperator(mat) {
			@Override
			public void operate(int row, int col, double[] pex) {
				pexs.add((int) pex[0]);
			}
		};
		int g1old = pexs.get(DataTool.getListMinIndex(pexs));
		int g2old = pexs.get(DataTool.getListMaxIndex(pexs));
		for (int i = g1old; i < g2old; i += params) {
			final int vlaue = i;
			final Mat src = mat.clone();
			new ImageOperator(src) {
				@Override
				public void operate(int row, int col, double[] pex) {
					if ((int) pex[0] <= vlaue) {
						src.put(row, col, 255);
					} else {
						src.put(row, col, 0);
					}
				}
			};
			mats.add(src);
		}
		return mats.get((int) (mats.size() / 2));
	}

	public List<Mat> getMats() {
		return mats;
	}

	@Override
	public String getProcessName() {
		return chain.getProcessName() + ",自定义二值化";
	}

}
